//
//  NSURL+HACShield.m
//  HACRouter
//
//  Created by Chengjie on 2019/5/22.
//

#import "NSURL+HACShield.h"
#import "NSObject+HACSwizzling.h"

@implementation NSURL (HACShield)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        hac_swizzleInstanceMethod(hac_getClass(@"NS", @"URL"), @selector(initWithString:), @selector(hac_initWithString:));
        hac_swizzleClassMethod(hac_getClass(@"NS", @"URL"), @selector(URLWithString:), @selector(hac_URLWithString:));
    });
}

- (instancetype)hac_initWithString:(NSString *)URLString {
    if ([URLString isKindOfClass:NSString.class]) {
        if (URLString.length > 0) {
            return [self hac_initWithString:URLString];
        }
    } else if([URLString isKindOfClass:[NSURL class]]){
        NSString *absoluteString = ((NSURL *)URLString).absoluteString;
        return [self hac_initWithString:absoluteString];
    }
    return [self hac_initWithString:@""];
}

+ ( instancetype)hac_URLWithString:(NSString *)URLString {
    if ([URLString isKindOfClass:NSString.class]) {
        if (URLString.length > 0) {
            return [NSURL hac_URLWithString:URLString];
        }
    } else if([URLString isKindOfClass:[NSURL class]]){
        return (NSURL *)URLString;
    }
    return [NSURL hac_URLWithString:@""];
}

@end
